package tw.ds0218.widgets.reporttime;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.RemoteViews;
import android.widget.Toast;

public class ReportTime extends AppWidgetProvider {
	// log tag
    private static final String TAG = "ReportTime";
    
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		
		Toast.makeText(context, TAG+"_onUpdate", Toast.LENGTH_SHORT).show();
		
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {
		Toast.makeText(context, TAG+"_onReceive", Toast.LENGTH_SHORT).show();
		// v1.5 fix that doesn't call onDelete Action
		final String action = intent.getAction();
		if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
			final int appWidgetId = intent.getExtras().getInt(
					AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
			if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
				this.onDeleted(context, new int[] { appWidgetId });
			}
		} else {
			super.onReceive(context, intent);
			
		}
	}
	
	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		Toast.makeText(context, TAG+"onDelete", Toast.LENGTH_SHORT).show();
		super.onDeleted(context, appWidgetIds);
	}
	
	static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
            int appWidgetId, String titlePrefix) {
		Toast.makeText(context, TAG+"_updateAppWidget", Toast.LENGTH_SHORT).show();

        // Construct the RemoteViews object.  It takes the package name (in our case, it's our
        // package, but it needs this because on the other side it's the widget host inflating
        // the layout from our package).
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
        //views.setImageViewBitmap(R.id.image, getBitmap(context, titlePrefix) );

        // Tell the widget manager
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
	
	public static Bitmap getBitmap(Context context, String hm){
    	Bitmap bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.robot);
    	try{
    		/*
    		//URL url = new URL("http://www.avtokei.jp/images/clocks/"+ hm.charAt(0)+hm.charAt(1) + "/" + hm + ".jpg");
    		URL url = new URL("http://www.avtokei.jp");
    		URLConnection connection = url.openConnection();
    		BufferedReader in = new BufferedReader(
    			new InputStreamReader(connection.getInputStream())
    		);
    		
    		String str ="";
    		String hmkey = "";
    		while( (str=in.readLine())!= null){
    			if(str.indexOf(hm+".jpg")!=-1){
    				hmkey = str.substring(str.indexOf(hm+".jpg")+9, str.indexOf("\" width="));
    				System.out.println("xxxxxx "+hmkey);
    			}
    			
    		}
    		*/
    		URL url = new URL("http://www.117time.com/time_img/girl/"+hm+".jpg"); 
    		//url = new URL("http://www.avtokei.jp/images/clocks/"+ hm.charAt(0)+hm.charAt(1) + "/" + hm + ".jpg?"+ hmkey);
    		//url = new URL("http://www.avtokei.jp/images/clocks/21/2116.jpg?1258722044");
    		URLConnection conn = url.openConnection();
    		conn.connect();
    		InputStream is = conn.getInputStream();
    		bm = BitmapFactory.decodeStream(is);
    		Toast.makeText(context, TAG+"_getImageOK", Toast.LENGTH_SHORT).show();
    		is.close();
    	}catch(IOException e){
    		e.printStackTrace();
    	}
    	
		return bm;
    }
}